package com.iterator.config;

import com.iterator.mapper.web.aop.AopMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: Silence_W
 * @date: 2021-01-15 11:24
 **/
@Component
@Aspect
public class WxIteratorAop {
    @Autowired
    AopMapper aopMapper;

    @Pointcut("execution( * com.iterator.controller.wechat..showDetailList(..))")
    public void Login(){

    }

    @Around("Login()")
    public Object doAfter(ProceedingJoinPoint joinPoint) throws Throwable {

        Subject subject = SecurityUtils.getSubject();
        String  username = (String) subject.getPrincipal();
        Object proceed1 = null;

            proceed1= joinPoint.proceed();

        if (username==null){
            return proceed1;

        }
        Session session = subject.getSession();
        Integer goodsId = (Integer) session.getAttribute("GoodsId");
        Integer userId= aopMapper.selectUseridById(username);
       Integer nums =  aopMapper.selectCountByGood(goodsId,userId);
       if (nums>=1){
           aopMapper.updateFootPrint(goodsId, new Date(),userId);

       }    else {
           aopMapper.insertFootprint(userId, goodsId, new Date(), new Date(), 0);

       }
       return proceed1;
    }
}

